package com.service.lx;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.dao.lx.QaDAO;
import com.dao.lx.QaDAOMybatis;
import com.dao.lx.UserDAO;
import com.entity.Qa;
import com.entity.User;
import com.service.ServiceException;
import com.utils.Pager;

@Service
@Transactional(rollbackFor = Exception.class)
public class QaService {
	@Autowired
	private QaDAO qaDao;
	@Autowired
	private UserDAO userDao;
	@Autowired
	private QaDAOMybatis qaDaoM;
	/**
	 * 添加/修改/逻辑删除
	 * @param qa
	 * @throws ServiceException
	 */
	public void addAndUpdateQa(Qa qa) throws ServiceException{
		try {
			Qa qa2=qaDao.findOne(qa.getQaId());
			if(qa2==null){
				qa.setQaCount(new BigDecimal(1));
				qa.setQaIfdelete(new BigDecimal(1));
			}
			if(qa.getUserId1()!=null){
				User user=userDao.findOne(qa.getUserId1());
				if(user!=null){
					qa.setUser(user);
				}else{
					qa.setUser(null);
				}
			}
			qa.setQaIntodate(new Date());
			qaDao.save(qa);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	
	/**
	 * 删除
	 * @param qaId
	 * @throws ServiceException
	 */
	public void deleteQa(Long qaId) throws ServiceException{
		try {
			qaDao.delete(qaId);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}

	/**
	 * 逻辑删除
	 * @param qaId
	 */
	public void UpdateQaIfdelete(Long qaId) {
		Qa qa = qaDao.findOne(qaId);
		qa.setQaIfdelete(new BigDecimal(0));
		addAndUpdateQa(qa);
	}
	
	/**
	 * 批量删除
	 * @param qaIds
	 */
	public void deletArrIfDelete(List<Long> qaIds) {
		if(qaIds != null) {
			for(Long qaId: qaIds) {
				Qa qa = qaDao.findOne(qaId);
				qa.setQaIfdelete(new BigDecimal(0));
				addAndUpdateQa(qa);
			}
		}
	}

	/**
	 * 单条查询
	 * @param qaId
	 * @return
	 */
	public Qa findOne(Long qaId) {
		return qaDao.findOne(qaId);
	}

	/**
	 * 快速查询：分类
	 * @param qaClassify
	 * @param pagesize 页大小
	 * @return
	 */
	public Page<Qa> findQaClassify(String qaClassify,Integer curpage, Integer pagesize) {
		return qaDao.queryByQaClassify(qaClassify, new PageRequest(curpage-1, pagesize));
	}

	/**
	 * 快速查询：问题
	 * @param classify
	 * @param pagesize 页大小
	 * @param classify
	 * @return
	 */
	public Page<Qa> findQaQuestion(String qaQuestion, Integer curpage, Integer pagesize) {
		return qaDao.queryByQaQuestion("%"+qaQuestion+"%", new PageRequest(curpage-1, pagesize));
	}
	
	/**
	 * 查询所有
	 * @param pagesize 页大小
	 * @return
	 */
	public Page<Qa> findAllQas(Integer curpage, Integer pagesize){
		return qaDao.findAllQaIfdelete1(new PageRequest(curpage-1, pagesize));
	}
	
	/**
	 * 快速查询（全部数据，分类，问题）
	 * @param field
	 * @param pagesize
	 * @return
	 */
	public Page<Qa> findFastAllQas(String field, Integer curpage, Integer pagesize){
		String str;
		if(field.startsWith("qaQuestion")){
			str=field.replace("qaQuestion", "");
			return findQaQuestion(str, curpage, pagesize);
		}else if(field.startsWith("qaClassify")){
			str=field.replace("qaClassify", "");
			return findQaClassify(str, curpage, pagesize);
		}else{
			return findAllQas(curpage, pagesize);
		}
	}
	
	/**
	 * 高级查询
	 * @param qa
	 * @return
	 */
	public Pager<Qa> findHighAllQas(Map map){
		Qa qa = (Qa) JSONObject.parseObject(map.get("list").toString(), Qa.class);
		Integer curpage=(Integer)map.get("curpage");
		Integer pagesize=(Integer)map.get("pagesize");
		List<Qa> list=(List<Qa>) qaDaoM.findTrim(qa);
		Pager<Qa> pager=new Pager<Qa>();
		if(list.size()!=0){
			list.get(list.size()-1);
			Integer start=(curpage-1)*pagesize;
			Integer stop=curpage*pagesize;
			if(list.size()<stop){
				stop=list.size();
			}
			if(list.size()<start){
				start=stop-list.size()%pagesize;
			}
			pager.setList(list.subList(start, stop));
			pager.setTotal(list.size());
		}else{
			pager.setList(new ArrayList());
			pager.setTotal(0);
		}
		return pager;
	}
}
